/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import model.CTDTM;
import model.CTGDM;
import util.DAL;
import util.MyConnectUnit;

public class CTGDD {
private MyConnectUnit conn;
	
	/**
	 * @function constructor MonHocD
	 * @throws Exception 
	 */
	public CTGDD() throws Exception {
		super();
		this.conn = DAL.getDAL();
	}

	/**
	 * @function get all MonHoc
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getAlls(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<CTGDM> ls = new ArrayList<CTGDM>();
		ResultSet rs = this.conn.Selects(
						"ctgd.id_ctgd, ctgd.ten_van_tat, ctgd.ten_day_du, ctgd.nam_bd_tuyen_sinh, ctgd.nam_kt_tuyen_sinh, ctgd.ts_tc, ctgd.id_ctdt, ctdt.ten_day_du",
						"CTGD ctgd, CTDT ctdt",
						"ctgd.id_ctdt=ctdt.id_ctdt AND ctgd.ten_day_du LIKE '%" + ten + "%'",
						"ctgd.ten_van_tat ASC",
						limit);
		CTGDM l = null;
		while (rs.next()) {
			l = new CTGDM();
			l.setId_ctgd(rs.getInt("ctgd.id_ctgd"));
			l.setTen_van_tat(rs.getString("ctgd.ten_van_tat"));
			l.setTen_day_du(rs.getString("ctgd.ten_day_du"));
			l.setNam_bd_tuyen_sinh(rs.getInt("ctgd.nam_bd_tuyen_sinh"));
			l.setNam_kt_tuyen_sinh(rs.getInt("ctgd.nam_kt_tuyen_sinh"));
			l.setTs_tc(rs.getInt("ctgd.ts_tc"));
			
			l.setId_ctdt(rs.getInt("ctgd.id_ctdt"));
			CTDTM ctdt = new CTDTM();
			ctdt.setTen_day_du(rs.getString("ctdt.ten_day_du"));
			l.setTen_ctdt(ctdt);
			ls.add(l);
		}
		return ls;
	}
	
	/**
	 * @function get all MonHoc
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<CTGDM> ls = new ArrayList<CTGDM>();
		ResultSet rs = this.conn.Select("CTGD", Condition, Limit, OrderBy);
		CTGDM l = null;
		while (rs.next()) {
			l = new CTGDM();
			l.setId_ctgd(rs.getInt("id_ctgd"));
			l.setTen_van_tat(rs.getString("ten_van_tat"));
			l.setTen_day_du(rs.getString("ten_day_du"));
			l.setNam_bd_tuyen_sinh(rs.getInt("nam_bd_tuyen_sinh"));
			l.setNam_kt_tuyen_sinh(rs.getInt("nam_kt_tuyen_sinh"));
			l.setTs_tc(rs.getInt("ts_tc"));
			l.setId_ctdt(rs.getInt("id_ctdt"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all MonHoc with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all MonHoc with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all MonHoc no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten MonHoc
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> searchCTGD(String ten) throws Exception {
		ArrayList<CTGDM> list = getAll("ten_day_du LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all CTGD with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<CTGDM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<CTGDM> list = getAll("ten_day_du LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * function insert MonHoc
	 * @param mh
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(CTGDM ctgd) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten_van_tat", ctgd.getTen_van_tat());
		map.put("ten_day_du", ctgd.getTen_day_du());
		map.put("nam_bd_tuyen_sinh", ctgd.getNam_bd_tuyen_sinh());
		map.put("nam_kt_tuyen_sinh", ctgd.getNam_kt_tuyen_sinh());
		map.put("ts_tc", ctgd.getTs_tc());
		map.put("id_ctdt", ctgd.getId_ctdt());
		return this.conn.Insert("CTGD", map);
	}

	/**
	 * @function update MonHoc
	 * @param mh
	 * @return
	 * @throws Exception
	 */
	public boolean Update(CTGDM ctgd) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ten_van_tat", ctgd.getTen_van_tat());
		map.put("ten_day_du", ctgd.getTen_day_du());
		map.put("nam_bd_tuyen_sinh", ctgd.getNam_bd_tuyen_sinh());
		map.put("nam_kt_tuyen_sinh", ctgd.getNam_kt_tuyen_sinh());
		map.put("ts_tc", ctgd.getTs_tc());
		map.put("id_ctdt", ctgd.getId_ctdt());
		return this.conn.Update("CTGD", map, "id_ctgd = " + ctgd.getId_ctgd());
	}

	/**
	 * @function delete MonHoc
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("CTGD", "id_ctgd = " + id);
	}
}
